//******************************************************************************
//   MSP430F530x Demo - Software Toggle PU.0/1
//
//   Description: Toggle PU.0/1 by xor'ing PU.0/1 inside of a software loop. In 
//   the example code, initial configuration is PU.0 = low and PU.1 = high. 
//    
//   Note: Since the LDO is not enabled in this case, an external connection 
//    between the device VCC and LDOO pin is required for the PortU operation
//
//   ACLK = 32.768kHz, MCLK = SMCLK = default DCO~1MHz
//
//                MSP430F530x
//             -----------------
//         /|\|              VCC|--
//          | |                 |  |
//          --|RST          LDOO|<-
//            |                 |
//            |             PU.0|--> Toggle
//            |             PU.1|--> Toggle (complementary to PU.0)
//
//   B. Nisarga
//   Texas Instruments Inc.
//   Dec 2010
//   Built with CCSv4 and IAR Embedded Workbench Version: 4.21
//******************************************************************************
#include <msp430f5309.h>    // To include latest F530x and F5310 header file v1.1, refer
#include "Driver/CC2500.h"                            // to the "ReadMe - Header" file in the code ex zip file
#include "Driver/led.h"

volatile unsigned char rece_flag = 0;

void init_port()
{  
	P2SEL &= 0x00;             
	P2IES  = 0x00;
	P2IES |= 0x01;
	P2IE   = 0x01;
	P2DIR  = 0xfe;
	P2OUT = 0x00;
	
	P2REN |= BIT0;                            // Enable P2.0 internal resistance
	P2OUT &= ~BIT0;                            // Set P2.0 as pull-down resistance
	P2IES |= BIT0;                            // P2.0 Hi/lo edge
	P2IFG &= ~BIT0;                           // P2.0 IFG cleared
	P2IE |= BIT0;                             // P2.0 interrupt enabled
	
	P4SEL &= 0x00;  
	P4DIR  = 0x00;   
	P4DIR |= 0x4a; 
	P4REN  = 0x00;
	P4REN |= 0x04;
	P4OUT  = 0x04;              
}

void main(void)
{
	volatile unsigned int i;
	
	WDTCTL = WDTPW+WDTHOLD;                   // Stop WDT
	LED_config();
	LED1_set();
	
	init_port();
	
	SPI_SELECT();
	SPI_SET_CLK();
	SPI_DIS_SELECT();
	cc2500_Init(0x00);
	
	SpiStrobe(CCxxx0_SIDLE);
	cc2500_Init_WOR();
	SpiStrobe(CCxxx0_SXOFF);  
	SpiStrobe(CCxxx0_SPWD); 
	
    _BIS_SR(GIE);

//	__bis_SR_register(LPM3_bits + GIE); // Enter LPM3

	while (1) {
		
	}
	
/*	while(1)                                  // continuous loop
	{
//		if (rece_flag == 1) {
//			rece_flag = 0;
#if 0	
            while(GDO0) {
                Set_CCxx00Rxd(); 
            }
            SpiStrobe(CCxxx0_SIDLE);
            cc2500_REnter_WOR();

            P2IFG &= ~BIT0;
            P2IE  |= BIT0;
#endif

#if 0
            cc2500_Exit();
            Set_CCxx00Rxd();
            P2IFG &= ~BIT0;
            P2IE  |= BIT0;
#endif
//		}
	}*/
}

#pragma vector =PORT2_VECTOR
__interrupt void P2_sever()
{
    P2IE  &= ~BIT0;
    P2IFG &= ~BIT0;
    RFReceivePacket(rxBuffer);
	Set_CCxx00Rxd();

	//if((rxBuffer[4]==0x22)||(rxBuffer[4]==0x44)) 
 //   {
		LED1_toggle();
	//}
        
    if (rece_flag == 0) {
		rece_flag = 1;
	} 

    while(GDO0) {
        Set_CCxx00Rxd(); 
    }
    SpiStrobe(CCxxx0_SIDLE);
    cc2500_REnter_WOR();

    P2IFG &= ~BIT0;
    P2IE  |= BIT0;
}